﻿using SharpSoft.Lexing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpSoft.Documentation.Html.CSS
{
    /// <summary>
    /// Css块词法解析
    /// </summary>
    internal class CssBlockLexer : _TextLexer
    {
        public CssBlockLexer(TextSource source)
            : base(source)
        {
        }
        public CssBlockLexer(string source)
            : base(source)
        {

        }

        public CssBlockToken ReadToken()
        {
            StringBuilder sb = new StringBuilder(200);
            BlockTokenType type;
            int pos = this.Position;//记录开始的位置
            string content = "";
            SkipWhiteSpace();
            if (EOS())//已达到末尾
                return null;
            char c = Current;
            if (c == '{')//属性块开始
            {
                type = BlockTokenType.PropertyBlock;
                while (!EOS())
                {
                    c = Next();
                    if (c == '}')//属性块结束
                        break;
                    if (c == '"' || c == '\'')
                        sb.Append(ReadString());//字符串单独读取
                    else
                        sb.Append(c);
                }
                Next();
            }
            else//其他任何字符都当作是选择器的一部分
            {
                type = BlockTokenType.Selector;
                sb.Append(c);
                while (!EOS())
                {
                    c = Next();
                    if (c == '{')//遭遇属性块，选择器块结束
                        break;
                    else
                        sb.Append(c);
                }
            }
            content = sb.ToString();
            return new CssBlockToken(pos, content.Length, type, content);
        }
    }
}
